<?php
/*
 * 
 * implementeaza documentului
 * 
 */

class xmlDocument {
	/**
	 * handlerul documentului
	 * @access public 
	 */
	public $docHandler;

	/**
	 * nodul radacina al documentului
	 * @access private
	 */
	private $docRoot;

	/**
	 * nodul radacina al continutului documentului
	 * @access private
	 */
	public $docContentRoot;
	
	/**
	 * nodul radacina al sect info a documentului
	 * @access private
	 */
	public $docInfoRoot;

	/**
	 * numele documentului
	 * @access public
	 */
	public $docName;

	/**
	 * calea documentului
	 * @access public
	 */
	public $docPath;
	public $docPathTo;

	//========constante de erori===========
	const CREATEERROR = "The file could not be created:";
	const CLOSEERROR = "The file could not be closed:";
	const WRITEERROR = "Could not write to the file:";
	//========constante de erori===========

	/**
	 * constructor
	 * creaza un nou document(creaza un nou fisier pt acesta)
	 * @param string $docName numele capitolului
	 * @param string $docPath calea documentului
	 * @param string $extDTD calea catre doc DTD
	 * @return DOMElement or Exception if errors occur
	 */
	public function __construct($docName, $docPath) {
		//verificare parametri

		//se creaza noul fisier	 	
		$newDocumentName = $docPath . "/" . $docName.".xml";
		if (!file_exists($newDocumentName)) {			
			//creare document xml
			$newDocumentFile = fopen($newDocumentName, "w");
			$chapTitle = explode(".", $docName);
			if ($newDocumentFile) {
				$closedOk = fclose($newDocumentFile);
				if (!$closedOk) {
					throw new Exception(self :: CLOSEERROR);
				}
			} else {
				throw new Exception(self :: CREATEERROR);
			}

			try {		
				$newDocType = DOMImplementation :: createDocumentType("xmlDocument", NULL, "../../include/dtd/docSkeleton.dtd");
				$this->docHandler = & DOMImplementation :: createDocument(NULL, "xmlDocument", $newDocType);

				$this->docRoot = & $this->docHandler->documentElement;
				$this->docInfoRoot=& $this->docHandler->createElement('docInfo');
				$this->docRoot->appendChild($this->docInfoRoot);
				$this->docContentRoot = & $this->docHandler->createElement('docBody');
				$this->docRoot->appendChild($this->docContentRoot);
				$this->docPath = $newDocumentName;
				$this->docPathTo = $docPath;
				$this->docHandler->save($this->docPath);

				$this->docHandler->resolveExternals = TRUE;
			} catch (exception $e) {
				echo $e->getmessage();
			}
		} else {
			//echo 'documentul deja exista';
			$this->docHandler=new DOMDocument('1.0');
			$this->docHandler->load($newDocumentName);
			$this->docRoot=& $this->docHandler->documentElement;
			$this->docPath=$newDocumentName;
			$this->docPathTo=$docPath;
			$docInfoRootList=$this->docHandler->getElementsByTagName('docInfo');
			foreach($docInfoRootList as $list){
				$this->docInfoRoot=$list;
			}
			$docContentRootList=$this->docHandler->getElementsByTagName('docBody');
			foreach($docContentRootList as $list){
				$this->docContentRoot=$list;
			}
		}
	}

	/**
	 * creare capitol nou
	 * @param $string $chapName numele noului capitol
	 * @param $string $chapId id-ul capitolului
	 * @access public
	 * @return bool TRUE if no error 
	 */
	public function createNewChapter($chapName, $chapId) {
		try {
			$newChapter = new xmlChapter($chapName, $chapId, $this->docPathTo);
			
			$newChapterContent=$this->docHandler->importNode($newChapter->chapterDoc->firstChild,true);
			$this->docContentRoot->appendChild($newChapterContent);
			
			$this->docHandler->save($this->docPath);
			return $newChapter;
		} catch (exception $e) {
			echo $e->getmessage();
			return false;
		}
	}

	public function reloadChapter($id, $chapterPath) {
		$chapters = $this->docContentRoot->getElementsByTagName('chapter');
		$oldChapter = 0;
		foreach ($chapters as $ch) {
			if ($ch->getAttribute('chapId') == $id) {
				$oldChapter = $ch;
			}
		}
		$newChapterContent=new DOMDocument('1.0');
		$newChapterContent->load($chapterPath);
		$newChapter = $this->docHandler->importNode($newChapterContent->firstChild, true);

		$this->docContentRoot->replaceChild($newChapter, $oldChapter);
		$this->docHandler->save($this->docPath);
	}
	
	public function getAllChapters(){
		$chaps=array();
		$chapsList=$this->docContentRoot->getElementsByTagName('chapter');
		foreach ($chapsList as $list){
			$titleList=$list->getElementsByTagName('title');
			$authorList=$list->getElementsByTagName('chapterAuthor');
			$title='';
			$author='';
			foreach ($titleList as $lisstt){
				$title=$lisstt->nodeValue;
			}			
			foreach ($authorList as $lisstt){
				$author=$lisstt->nodeValue;
			}
			
			$chapId=$list->getAttribute('chapId');
			
			$chaps[$chapId]['title']=$title;
			$chaps[$chapId]['author']=$author;
		}
		return $chaps;
	}

}
?>